package common

import "strconv"

type Page struct {
	Pi, Ps int
}

func (p Page) Limit() string {
	if p.Ps <= 0 {
		p.Ps = 20
	}
	if p.Pi <= 0 {
		p.Pi = 0
	}
	return " limit " + strconv.Itoa(p.Pi*p.Ps) + "," + strconv.Itoa(p.Ps)
}

//OrderBy Sql order by
type OrderBy struct {
	Field string
	Asc   bool
}

func (o OrderBy) Sql() string {
	if o.Asc {
		return o.Field
	}

	return o.Field + " desc"
}

type OrderBys []OrderBy

//NewOrderBys fas : Filed Asc...
func NewOrderBys(fas ...interface{}) OrderBys {
	n := len(fas)
	r := make([]OrderBy, n/2)
	for i := 0; i < n; i += 2 {
		r[i] = OrderBy{Field: fas[i].(string), Asc: fas[i+1].(bool)}
	}
	return r
}
func (os OrderBys) Sql() string {
	s := ""
	for i, v := range os {
		s += v.Sql()
		if i < len(os)-1 {
			s += ","
		}
	}
	return s
}
